home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / src / insdel.c < prev    next >
C/C++ Source or Header  |  1993-10-04  |  17KB  |  659 lines

  1. /* Buffer insertion/deletion and gap motion for GNU Emacs.
  2.    Copyright (C) 1985, 1986, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include "config.h"
  22. #include "lisp.h"
  23. #include "intervals.h"
  24. #include "buffer.h"
  25. #include "window.h"
  26. #include "blockinput.h"
  27.  
  28. #ifdef STDC_HEADERS
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #endif
  32. #include "insdel_p.h"
  33. #include "keyboard_p.h"
  34. #include "alloc_p.h"
  35. #include "eval_p.h"
  36. #include "undo_p.h"
  37. #include "buffer_p.h"
  38. static _VOID_ gap_left _P_((register int pos, int newgap));
  39. static _VOID_ gap_right _P_((register int pos));
  40. static Lisp_Object before_change_function_restore _P_((Lisp_Object value));
  41. static Lisp_Object after_change_function_restore _P_((Lisp_Object value));
  42. static _VOID_ signal_before_change _P_((Lisp_Object start, Lisp_Object end));
  43.  
  44. /* Add `amount' to the position of every marker in the current buffer
  45.    whose current position is between `from' (exclusive) and `to' (inclusive).
  46.    Also, any markers past the outside of that interval, in the direction
  47.    of adjustment, are first moved back to the near end of the interval
  48.    and then adjusted by `amount'.  */
  49.  
  50. static _VOID_
  51. adjust_markers (from, to, amount)
  52.      register int from, to, amount;
  53. {
  54.   Lisp_Object marker;
  55.   register struct Lisp_Marker *m;
  56.   register int mpos;
  57.  
  58.   marker = current_buffer->markers;
  59.  
  60.   while (!NILP (marker))
  61.     {
  62.       m = XMARKER (marker);
  63.       mpos = m->bufpos;
  64.       if (amount > 0)
  65.     {
  66.       if (mpos > to && mpos < to + amount)
  67.         mpos = to + amount;
  68.     }
  69.       else
  70.     {
  71.       if (mpos > from + amount && mpos <= from)
  72.         mpos = from + amount;
  73.     }
  74.       if (mpos > from && mpos <= to)
  75.     mpos += amount;
  76.       m->bufpos = mpos;
  77.       marker = m->chain;
  78.     }
  79. }
  80.  
  81. /* Move the gap to POS, which is less than the current GPT.
  82.    If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged.  */
  83.  
  84. static _VOID_
  85. gap_left (pos, newgap)
  86.      register int pos;
  87.      int newgap;
  88. {
  89.   register unsigned char *to, *from;
  90.   register int i;
  91.   int new_s1;
  92.  
  93.   pos--;
  94.  
  95.   if (!newgap)
  96.     {
  97.       if (unchanged_modified == MODIFF)
  98.     {
  99.       beg_unchanged = pos;
  100.       end_unchanged = Z - pos - 1;
  101.     }
  102.       else
  103.     {
  104.       if (Z - GPT < end_unchanged)
  105.         end_unchanged = Z - GPT;
  106.       if (pos < beg_unchanged)
  107.         beg_unchanged = pos;
  108.     }
  109.     }
  110.  
  111.   i = GPT;
  112.   to = GAP_END_ADDR;
  113.   from = GPT_ADDR;
  114.   new_s1 = GPT - BEG;
  115.  
  116.   /* Now copy the characters.  To move the gap down,
  117.      copy characters up.  */
  118.  
  119.   while (1)
  120.     {
  121.       /* I gets number of characters left to copy.  */
  122.       i = new_s1 - pos;
  123.       if (i == 0)
  124.     break;
  125.       /* If a quit is requested, stop copying now.
  126.      Change POS to be where we have actually moved the gap to.  */
  127.       if (QUITP)
  128.     {
  129.       pos = new_s1;
  130.       break;
  131.     }
  132.       /* Move at most 32000 chars before checking again for a quit.  */
  133.       if (i > 32000)
  134.     i = 32000;
  135. #ifdef GAP_USE_MEMMOVE
  136.       new_s1 -= i;
  137.       from -= i;
  138.       to -= i;
  139.       memmove(to, from, i);
  140. #else
  141. #ifdef GAP_USE_BCOPY
  142.       if (i >= 128
  143.       /* bcopy is safe if the two areas of memory do not overlap
  144.          or on systems where bcopy is always safe for moving upward.  */
  145.       && (BCOPY_UPWARD_SAFE
  146.           || to - from >= 128))
  147.     {
  148.       /* If overlap is not safe, avoid it by not moving too many
  149.          characters at once.  */
  150.       if (!BCOPY_UPWARD_SAFE && i > to - from)
  151.         i = to - from;
  152.       new_s1 -= i;
  153.       from -= i, to -= i;
  154.       bcopy (from, to, i);
  155.     }
  156.       else
  157. #endif
  158.     {
  159.       new_s1 -= i;
  160.       while (--i >= 0)
  161.         *--to = *--from;
  162.     }
  163. #endif
  164.     }
  165.  
  166.   /* Adjust markers, and buffer data structure, to put the gap at POS.
  167.      POS is where the loop above stopped, which may be what was specified
  168.      or may be where a quit was detected.  */
  169.   adjust_markers (pos + 1, GPT, GAP_SIZE);
  170.   GPT = pos + 1;
  171.   QUIT;
  172. }
  173.  
  174. static _VOID_
  175. gap_right (pos)
  176.      register int pos;
  177. {
  178.   register unsigned char *to, *from;
  179.   register int i;
  180.   int new_s1;
  181.  
  182.   pos--;
  183.  
  184.   if (unchanged_modified == MODIFF)
  185.     {
  186.       beg_unchanged = pos;
  187.       end_unchanged = Z - pos - 1;
  188.     }
  189.   else
  190.     {
  191.       if (Z - pos - 1 < end_unchanged)
  192.     end_unchanged = Z - pos - 1;
  193.       if (GPT - BEG < beg_unchanged)
  194.     beg_unchanged = GPT - BEG;
  195.     }
  196.  
  197.   i = GPT;
  198.   from = GAP_END_ADDR;
  199.   to = GPT_ADDR;
  200.   new_s1 = GPT - 1;
  201.  
  202.   /* Now copy the characters.  To move the gap up,
  203.      copy characters down.  */
  204.  
  205.   while (1)
  206.     {
  207.       /* I gets number of characters left to copy.  */
  208.       i = pos - new_s1;
  209.       if (i == 0)
  210.     break;
  211.       /* If a quit is requested, stop copying now.
  212.      Change POS to be where we have actually moved the gap to.  */
  213.       if (QUITP)
  214.     {
  215.       pos = new_s1;
  216.       break;
  217.     }
  218.       /* Move at most 32000 chars before checking again for a quit.  */
  219.       if (i > 32000)
  220.     i = 32000;
  221. #ifdef GAP_USE_MEMMOVE
  222.       new_s1 += i;
  223.       memmove(to, from, i);
  224.       from += i;
  225.       to += i;
  226. #else
  227. #ifdef GAP_USE_BCOPY
  228.       if (i >= 128
  229.       /* bcopy is safe if the two areas of memory do not overlap
  230.          or on systems where bcopy is always safe for moving downward.  */
  231.       && (BCOPY_DOWNWARD_SAFE
  232.           || from - to >= 128))
  233.     {
  234.       /* If overlap is not safe, avoid it by not moving too many
  235.          characters at once.  */
  236.       if (!BCOPY_DOWNWARD_SAFE && i > from - to)
  237.         i = from - to;
  238.       new_s1 += i;
  239.       bcopy (from, to, i);
  240.       from += i, to += i;
  241.     }
  242.       else
  243. #endif
  244.     {
  245.       new_s1 += i;
  246.       while (--i >= 0)
  247.         *to++ = *from++;
  248.     }
  249. #endif
  250.     }
  251.  
  252.   adjust_markers (GPT + GAP_SIZE, pos + 1 + GAP_SIZE, - GAP_SIZE);
  253.   GPT = pos + 1;
  254.   QUIT;
  255. }
  256.  
  257. /* Move gap to position `pos'.
  258.    Note that this can quit!  */
  259.  
  260. _VOID_
  261. move_gap (pos)
  262.      int pos;
  263. {
  264.   if (pos < GPT)
  265.     gap_left (pos, 0);
  266.   else if (pos > GPT)
  267.     gap_right (pos);
  268. }
  269.  
  270.  
  271. /* Make the gap INCREMENT characters longer.  */
  272.  
  273. _VOID_
  274. make_gap (increment)
  275.      int increment;
  276. {
  277.   unsigned char *result;
  278.   Lisp_Object tem;
  279.   int real_gap_loc;
  280.   int old_gap_size;
  281.  
  282.   /* If we have to get more space, get enough to last a while.  */
  283.   increment += 2000;
  284.  
  285.   BLOCK_INPUT;
  286.   result = BUFFER_REALLOC (BEG_ADDR, (Z - BEG + GAP_SIZE + increment));
  287.   UNBLOCK_INPUT;
  288.  
  289.   if (result == 0)
  290.     memory_full ();
  291.   BEG_ADDR = result;
  292.  
  293.   /* Prevent quitting in move_gap.  */
  294.   tem = Vinhibit_quit;
  295.   Vinhibit_quit = Qt;
  296.  
  297.   real_gap_loc = GPT;
  298.   old_gap_size = GAP_SIZE;
  299.  
  300.   /* Call the newly allocated space a gap at the end of the whole space.  */
  301.   GPT = Z + GAP_SIZE;
  302.   GAP_SIZE = increment;
  303.  
  304.   /* Move the new gap down to be consecutive with the end of the old one.
  305.      This adjusts the markers properly too.  */
  306.   gap_left (real_gap_loc + old_gap_size, 1);
  307.  
  308.   /* Now combine the two into one large gap.  */
  309.   GAP_SIZE += old_gap_size;
  310.   GPT = real_gap_loc;
  311.  
  312.   Vinhibit_quit = tem;
  313. }
  314.  
  315.  
  316. static Lisp_Object
  317. before_change_function_restore (value)
  318.      Lisp_Object value;
  319. {
  320.   Vbefore_change_function = value;
  321.   return Vbefore_change_function;
  322. }
  323.  
  324. static Lisp_Object
  325. after_change_function_restore (value)
  326.      Lisp_Object value;
  327. {
  328.   Vafter_change_function = value;
  329.   return Vafter_change_function;
  330. }
  331.  
  332. /* Signal a change immediately after it happens.
  333.    POS is the address of the start of the changed text.
  334.    LENDEL is the number of characters of the text before the change.
  335.    (Not the whole buffer; just the part that was changed.)
  336.    LENINS is the number of characters in the changed text.  */
  337.  
  338. _VOID_
  339. signal_after_change (pos, lendel, lenins)
  340.      int pos, lendel, lenins;
  341. {
  342.   if (!NILP (Vafter_change_function))
  343.     {
  344.       int count = specpdl_ptr - specpdl;
  345.       Lisp_Object function;
  346.       function = Vafter_change_function;
  347.  
  348.       record_unwind_protect (after_change_function_restore,
  349.                  Vafter_change_function);
  350.       record_unwind_protect (before_change_function_restore,
  351.                  Vbefore_change_function);
  352.       Vafter_change_function = Qnil;
  353.       Vbefore_change_function = Qnil;
  354.  
  355.       call3 (function, make_number (pos), make_number (pos + lenins),
  356.          make_number (lendel));
  357.       unbind_to (count, Qnil);
  358.     }
  359. }
  360.  
  361.  
  362. /* Insert a string of specified length before point.
  363.    DO NOT use this for the contents of a Lisp string!
  364.    prepare_to_modify_buffer could relocate the string.  */
  365.  
  366. _VOID_
  367. insert (string, length)
  368.      register unsigned char *string;
  369.      register length;
  370. {
  371.   register Lisp_Object temp;
  372.  
  373.   if (length < 1)
  374.     return;
  375.  
  376.   /* Make sure point-max won't overflow after this insertion.  */
  377.   XSET (temp, Lisp_Int, length + Z);
  378.   if (length + Z != XINT (temp))
  379.     error ("maximum buffer size exceeded");
  380.  
  381.   prepare_to_modify_buffer (point, point);
  382.  
  383.   if (point != GPT)
  384.     move_gap (point);
  385.   if (GAP_SIZE < length)
  386.     make_gap (length - GAP_SIZE);
  387.  
  388.   record_insert (point, length);
  389.   MODIFF++;
  390.  
  391.   bcopy (string, GPT_ADDR, length);
  392.  
  393.   /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
  394.   offset_intervals (current_buffer, point, length);
  395.  
  396.   GAP_SIZE -= length;
  397.   GPT += length;
  398.   ZV += length;
  399.   Z += length;
  400.   SET_PT (point + length);
  401.  
  402.   signal_after_change (point-length, 0, length);
  403. }
  404.  
  405. /* Insert the part of the text of STRING, a Lisp object assumed to be
  406.    of type string, consisting of the LENGTH characters starting at
  407.    position POS.  If the text of STRING has properties, they are absorbed
  408.    into the buffer.
  409.  
  410.    It does not work to use `insert' for this, because a GC could happen
  411.    before we bcopy the stuff into the buffer, and relocate the string
  412.    without insert noticing.  */
  413.  
  414. _VOID_
  415. insert_from_string (string, pos, length)
  416.      Lisp_Object string;
  417.      register int pos, length;
  418. {
  419.   register Lisp_Object temp;
  420.   struct gcpro gcpro1;
  421.  
  422.   if (length < 1)
  423.     return;
  424.  
  425.   /* Make sure point-max won't overflow after this insertion.  */
  426.   XSET (temp, Lisp_Int, length + Z);
  427.   if (length + Z != XINT (temp))
  428.     error ("maximum buffer size exceeded");
  429.  
  430.   GCPRO1 (string);
  431.   prepare_to_modify_buffer (point, point);
  432.  
  433.   if (point != GPT)
  434.     move_gap (point);
  435.   if (GAP_SIZE < length)
  436.     make_gap (length - GAP_SIZE);
  437.  
  438.   record_insert (point, length);
  439.   MODIFF++;
  440.   UNGCPRO;
  441.  
  442.   bcopy (XSTRING (string)->data, GPT_ADDR, length);
  443.  
  444.   /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
  445.   offset_intervals (current_buffer, point, length);
  446.  
  447.   GAP_SIZE -= length;
  448.   GPT += length;
  449.   ZV += length;
  450.   Z += length;
  451.  
  452.   /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
  453.   graft_intervals_into_buffer (XSTRING (string)->intervals, point,
  454.                    current_buffer);
  455.  
  456.   SET_PT (point + length);
  457.  
  458.   signal_after_change (point-length, 0, length);
  459. }
  460.  
  461. /* Insert the character C before point */
  462.  
  463. _VOID_
  464. insert_char (c)
  465.      unsigned char c;
  466. {
  467.   insert (&c, 1);
  468. }
  469.  
  470. /* Insert the null-terminated string S before point */
  471.  
  472. _VOID_
  473. insert_string (s)
  474.      char *s;
  475. {
  476.   insert (s, strlen (s));
  477. }
  478.  
  479. /* Like `insert' except that all markers pointing at the place where
  480.    the insertion happens are adjusted to point after it.
  481.    Don't use this function to insert part of a Lisp string,
  482.    since gc could happen and relocate it.  */
  483.  
  484. _VOID_
  485. insert_before_markers (string, length)
  486.      unsigned char *string;
  487.      register int length;
  488. {
  489.   register int opoint = point;
  490.   insert (string, length);
  491.   adjust_markers (opoint - 1, opoint, length);
  492. }
  493.  
  494. /* Insert part of a Lisp string, relocating markers after.  */
  495.  
  496. _VOID_
  497. insert_from_string_before_markers (string, pos, length)
  498.      Lisp_Object string;
  499.      register int pos, length;
  500. {
  501.   register int opoint = point;
  502.   insert_from_string (string, pos, length);
  503.   adjust_markers (opoint - 1, opoint, length);
  504. }
  505.  
  506. /* Delete characters in current buffer
  507.    from FROM up to (but not including) TO.  */
  508.  
  509. _VOID_
  510. del_range (from, to)
  511.      register int from, to;
  512. {
  513.   register int numdel;
  514.  
  515.   /* Make args be valid */
  516.   if (from < BEGV)
  517.     from = BEGV;
  518.   if (to > ZV)
  519.     to = ZV;
  520.  
  521.   if ((numdel = to - from) <= 0)
  522.     return;
  523.  
  524.   /* Make sure the gap is somewhere in or next to what we are deleting.  */
  525.   if (from > GPT)
  526.     gap_right (from);
  527.   if (to < GPT)
  528.     gap_left (to, 0);
  529.  
  530.   prepare_to_modify_buffer (from, to);
  531.  
  532.   record_delete (from, numdel);
  533.   MODIFF++;
  534.  
  535.   /* Relocate point as if it were a marker.  */
  536.   if (from < point)
  537.     {
  538.       if (point < to)
  539.     SET_PT (from);
  540.       else
  541.     SET_PT (point - numdel);
  542.     }
  543.  
  544.   /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
  545.   offset_intervals (current_buffer, point, - numdel);
  546.  
  547.   /* Relocate all markers pointing into the new, larger gap
  548.      to point at the end of the text before the gap.  */
  549.   adjust_markers (to + GAP_SIZE, to + GAP_SIZE, - numdel - GAP_SIZE);
  550.  
  551.   GAP_SIZE += numdel;
  552.   ZV -= numdel;
  553.   Z -= numdel;
  554.   GPT = from;
  555.  
  556.   if (GPT - BEG < beg_unchanged)
  557.     beg_unchanged = GPT - BEG;
  558.   if (Z - GPT < end_unchanged)
  559.     end_unchanged = Z - GPT;
  560.  
  561.   signal_after_change (from, numdel, 0);
  562. }
  563.  
  564. /* Call this if you're about to change the region of BUFFER from START
  565.    to END.  This checks the read-only properties of the region, calls
  566.    the necessary modification hooks, and warns the next redisplay that
  567.    it should pay attention to that area.  */
  568. _VOID_
  569. modify_region (buffer, start, end)
  570.      struct buffer *buffer;
  571.      int start, end;
  572. {
  573.   struct buffer *old_buffer = current_buffer;
  574.  
  575.   if (buffer != old_buffer)
  576.     set_buffer_internal (buffer);
  577.  
  578.   prepare_to_modify_buffer (start, end);
  579.  
  580.   if (start - 1 < beg_unchanged || unchanged_modified == MODIFF)
  581.     beg_unchanged = start - 1;
  582.   if (Z - end < end_unchanged
  583.       || unchanged_modified == MODIFF)
  584.     end_unchanged = Z - end;
  585.   MODIFF++;
  586.  
  587.   if (buffer != old_buffer)
  588.     set_buffer_internal (old_buffer);
  589. }
  590.  
  591. /* Signal a change to the buffer immediately before it happens.
  592.    START and END are the bounds of the text to be changed,
  593.    as Lisp objects.  */
  594.  
  595. static _VOID_
  596. signal_before_change (start, end)
  597.      Lisp_Object start, end;
  598. {
  599.   /* If buffer is unmodified, run a special hook for that case.  */
  600.   if (current_buffer->save_modified >= MODIFF
  601.       && !NILP (Vfirst_change_hook)
  602.       && !NILP (Vrun_hooks))
  603.     call1 (Vrun_hooks, Qfirst_change_hook);
  604.  
  605.   /* Now in any case run the before-change-function if any.  */
  606.   if (!NILP (Vbefore_change_function))
  607.     {
  608.       int count = specpdl_ptr - specpdl;
  609.       Lisp_Object function;
  610.  
  611.       function = Vbefore_change_function;
  612.       record_unwind_protect (after_change_function_restore,
  613.                  Vafter_change_function);
  614.       record_unwind_protect (before_change_function_restore,
  615.                  Vbefore_change_function);
  616.       Vafter_change_function = Qnil;
  617.       Vbefore_change_function = Qnil;
  618.  
  619.       call2 (function, start, end);
  620.       unbind_to (count, Qnil);
  621.     }
  622. }
  623.  
  624. /* Check that it is okay to modify the buffer between START and END.
  625.    Run the before-change-function, if any.  If intervals are in use,
  626.    verify that the text to be modified is not read-only, and call
  627.    any modification properties the text may have. */
  628.  
  629. _VOID_
  630. prepare_to_modify_buffer (start, end)
  631.      Lisp_Object start, end;
  632. {
  633.   if (!NILP (current_buffer->read_only))
  634.     Fbarf_if_buffer_read_only ();
  635.  
  636.   /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
  637.   verify_interval_modification (current_buffer, start, end);
  638.  
  639.   verify_overlay_modification (start, end);
  640.  
  641. #ifdef CLASH_DETECTION
  642.   if (!NILP (current_buffer->filename)
  643.       && current_buffer->save_modified >= MODIFF)
  644.     lock_file (current_buffer->filename);
  645. #else
  646.   /* At least warn if this file has changed on disk since it was visited.  */
  647.   if (!NILP (current_buffer->filename)
  648.       && current_buffer->save_modified >= MODIFF
  649.       && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
  650.       && !NILP (Ffile_exists_p (current_buffer->filename)))
  651.     call1 (intern ("ask-user-about-supersession-threat"),
  652.        current_buffer->filename);
  653. #endif /* not CLASH_DETECTION */
  654.  
  655.   signal_before_change (start, end);
  656.  
  657.   Vdeactivate_mark = Qt;
  658. }
  659.